<?php
class RDFIOStore {
    protected $m_store;

    function __construct() {
        global $smwgARC2StoreConfig;
        $this->m_arcstore = ARC2::getStore($smwgARC2StoreConfig);
    }

    function getOrigURIURI() {
        return $this->getURIResolverURI() . 'Property-3AOriginal_URI';
    }

    function getOrigURI( $uri ) {
        $origuri = '';
        $store = $this->m_arcstore;
        $origuriuri = $this->getOrigURIURI();
        $q = "SELECT ?origuri WHERE { <$uri> <$origuriuri> ?origuri }";
        $rs = $store->query($q);
        if (!$store->getErrors()) {
            $rows = $rs['result']['rows'];
            $row = $rows[0];
            $origuri = $row['origuri'];
        } else {
            die( "Error in ARC Store: " . print_r( $store->getErrors(), true ) );
        }
        return $origuri;
    }

    function getURIForOrigURI( $origuri ) {
        $uri = '';
        $store = $this->m_arcstore;
        $origuriuri = $this->getOrigURIURI();
        $q = "SELECT ?uri WHERE { ?uri <$origuriuri> <$origuri> }";
        $rs = $store->query($q);
        if (!$store->getErrors()) {
            $rows = $rs['result']['rows'];
            $row = $rows[0];
            $uri = $row['uri'];
        } else {
            die( "Error in ARC Store: " . print_r( $store->getErrors(), true ) );
        }
        return $uri;
    }

    function getURIResolverURI() {
        global $wgServer, $wgScriptPath;
        $uriresolveruri = $wgServer . $wgScriptPath . '/index.php/Special:URIResolver/';
        return $uriresolveruri;
    }
    
    function getWikiTitleByOriginalURI( $uri ) {
        $wikititleResolverURI = $this->getURIForOrigURI( $uri );
        $resolverURI = $this->getURIResolverURI();
        $wikiTitle = str_replace( $resolverURI, '', $wikititleResolverURI );
        $wikiTitle = str_replace( 'Property-3A', '', $wikiTitle );
        $wikiTitle = str_replace( 'Property:', '', $wikiTitle );
        return $wikiTitle;
    }
}